package controllers

import (
	"encoding/json"
	"new/kpg/app"
	"new/kpg/e"
	"new/models"
	"new/services"

	"github.com/gin-gonic/gin"
	"github.com/unknwon/com"
)

//查找体检表信息
func GetPhysical_exam(c *gin.Context) {
	//fmt.Println("用于测试")
	err, info, total := services.GetAllphysical_exam()
	if err != nil {
		app.Error(c, e.ERROR, err, err.Error())
		return
	}
	app.OK(c, map[string]interface{}{"value": info, "total": total}, "查询成功")
}

//删除体检表信息
func DelPhysical_exam(c *gin.Context) {
	b, _ := c.GetRawData()
	var m map[string]string
	_ = json.Unmarshal(b, &m)
	id := com.StrTo(m["Physical_examPno"]).MustInt()
	if id == -1 {
		app.INFO(c, 30001, "参数错误")
		return
	}
	err := services.DelPhysical_exam(id)
	if err != nil {
		app.Error(c, e.ERROR, err, err.Error())
		return
	}
	app.OK(c, map[string]interface{}{}, "删除成功")
}

//更改体检表信息
func UpdatePhysical_exam(c *gin.Context) {
	b, _ := c.GetRawData()
	var m map[string]string
	_ = json.Unmarshal(b, &m)
	if m["Pno"] == "" {
		app.INFO(c, 30000, "参数非法")
		return
	}
	Pno := m["Pno"]
	Sno := m["Sno"]
	Page := m["Page"]
	Pheight := m["Pheight"]
	Pweight := m["Pweight"]
	Pchest := m["Pchest"]
	Pdate := m["Pdate"]

	if Pno == "" {
		app.INFO(c, 30000, "参数非法")
		return
	}
	err := services.UpdatePhysical_exam(models.Physical_exam{Pno, Sno, Page, Pheight, Pweight, Pchest, Pdate})
	if err != nil {
		app.Error(c, e.ERROR, err, err.Error())
		return
	}
	app.OK(c, map[string]interface{}{}, "更新成功")
}

//增加体检表信息
func AddPhysical_exam(c *gin.Context) {
	b, _ := c.GetRawData()
	var m map[string]string
	_ = json.Unmarshal(b, &m)
	Pno := m["Pno"]
	Sno := m["Sno"]
	Page := m["Page"]
	Pheight := m["Pheight"]
	Pweight := m["Pweight"]
	Pchest := m["Pchest"]
	Pdate := m["Pdate"]
	if Pno == "" {
		app.INFO(c, 30000, "参数非法")
		return
	}
	err := services.AddPhysical_exam(models.Physical_exam{Pno, Sno, Page, Pheight, Pweight, Pchest, Pdate})
	if err != nil {
		app.Error(c, e.ERROR, err, err.Error())
		return
	}
	app.OK(c, map[string]interface{}{}, "添加成功")
}
